package cn.wolfcode.luowowo.website.controller;


import cn.wolfcode.luowowo.article.service.IFlightCityService;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.serach.query.FlightTemplateQueryObject;
import cn.wolfcode.luowowo.serach.service.IFlightSeivice;
import cn.wolfcode.luowowo.serach.template.CabinInfoTemplate;
import cn.wolfcode.luowowo.serach.template.FlightTemplate;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("flight")
public class FlightController {

    @Reference
    private IFlightSeivice flightSeivice;

    @Reference
    private IFlightCityService flightCityService;


    @RequestMapping("")
    public String index(Model model) {
        model.addAttribute("hotFlights", flightCityService.queryHotCity());
        model.addAttribute("initialA", flightCityService.queryCity("a", "e"));
        model.addAttribute("initialF", flightCityService.queryCity("f", "j"));
        model.addAttribute("initialK", flightCityService.queryCity("k", "p"));
        model.addAttribute("initialQ", flightCityService.queryCity("q", "w"));
        model.addAttribute("initialX", flightCityService.queryCity("x", "z"));

        return "flight/index";
    }

    @RequestMapping("search")
    public String search(@ModelAttribute("qo") FlightTemplateQueryObject qo, Model model) {


        List<FlightTemplate> list = flightSeivice.query(qo);

        List<String> takeoffPortNames = new ArrayList<>();
        List<String> landingPortNames = new ArrayList<>();
        List<String> airlineNames = new ArrayList<>();
        List<String> planeTypes = new ArrayList<>();

        for (FlightTemplate flightTemplate : list) {
            if (!takeoffPortNames.contains(flightTemplate.getTakeoffPortName())) {
                takeoffPortNames.add(flightTemplate.getTakeoffPortName());
            }
            if (!landingPortNames.contains(flightTemplate.getLandingPortName())) {
                landingPortNames.add(flightTemplate.getLandingPortName());
            }
            if (!airlineNames.contains(flightTemplate.getAirlineName())) {
                airlineNames.add(flightTemplate.getAirlineName());
            }
            if (!planeTypes.contains(flightTemplate.getPlaneType())) {
                planeTypes.add(flightTemplate.getPlaneType());
            }

        }

        model.addAttribute("list", list);
        model.addAttribute("takeoffPortNames", takeoffPortNames);
        model.addAttribute("landingPortNames", landingPortNames);
        model.addAttribute("airlineNames", airlineNames);
        model.addAttribute("planeTypes", planeTypes);

        return "/flight/searchTpl";
    }

    @RequestMapping("searchKickback")
    public String searchKickback(@ModelAttribute("qo")FlightTemplateQueryObject qo, Model model, Long fid) {


        System.out.println(fid);

        FlightTemplate ft = flightSeivice.findById(fid);
        System.out.println(ft);
        model.addAttribute("ft", ft);

        String fromCityCode = qo.getFromCityCode();
        String toCityCode = qo.getToCityCode();
        Date takeoffTime = qo.getTakeoffTime();
        Date returnTime = qo.getReturnTime();

        qo.setFromCityCode(toCityCode);
        qo.setToCityCode(fromCityCode);
        qo.setTakeoffTime(returnTime);
        qo.setReturnTime(null);

        List<FlightTemplate> list = flightSeivice.query(qo);

        List<String> takeoffPortNames = new ArrayList<>();
        List<String> landingPortNames = new ArrayList<>();
        List<String> airlineNames = new ArrayList<>();
        List<String> planeTypes = new ArrayList<>();

        for (FlightTemplate flightTemplate : list) {
            if (!takeoffPortNames.contains(flightTemplate.getTakeoffPortName())) {
                takeoffPortNames.add(flightTemplate.getTakeoffPortName());
            }
            if (!landingPortNames.contains(flightTemplate.getLandingPortName())) {
                landingPortNames.add(flightTemplate.getLandingPortName());
            }
            if (!airlineNames.contains(flightTemplate.getAirlineName())) {
                airlineNames.add(flightTemplate.getAirlineName());
            }
            if (!planeTypes.contains(flightTemplate.getPlaneType())) {
                planeTypes.add(flightTemplate.getPlaneType());
            }

        }

        model.addAttribute("list", list);
        model.addAttribute("takeoffPortNames", takeoffPortNames);
        model.addAttribute("landingPortNames", landingPortNames);
        model.addAttribute("airlineNames", airlineNames);
        model.addAttribute("planeTypes", planeTypes);

        return "/flight/searchTpl";
    }

    @RequestMapping("indent")
    public String indent(Model model, Long fid, Long cid, Long ftid) {
        FlightTemplate fttow = flightSeivice.findById(fid);
        model.addAttribute("fttow", fttow);

        for (CabinInfoTemplate ct : fttow.getCabinInfos()) {
            if (ct.getId() == cid) {
                if (ftid != null) {
                    BigDecimal bigDecimal = new BigDecimal("2");
                    ct.setNitTicketPrice(bigDecimal.multiply(ct.getNitTicketPrice()));
                }
                model.addAttribute("ct", ct);
            }
        }

        if (ftid != null) {
            FlightTemplate ftone = flightSeivice.findById(ftid);
            model.addAttribute("ftone", ftone);
        }
        return "/flight/indent";
    }

    @RequestMapping("paysuccess")
    public String paysuccess(Long fid, Long cid, Long ftid) {
        FlightTemplate fttow = flightSeivice.findById(fid);
        List<CabinInfoTemplate> cabinInfos = fttow.getCabinInfos();
        CabinInfoTemplate cabinInfoTemplate = new CabinInfoTemplate();
        int i = 0;
        for (CabinInfoTemplate ct : cabinInfos) {

            if (ct.getId() == cid) {
                ct.setCabin(ct.getCabin() - 1);
                cabinInfoTemplate = ct;
                break;
            }
            i++;
        }
        cabinInfos.remove(i);
        cabinInfos.add(cabinInfoTemplate);
        fttow.setCabinInfos(cabinInfos);
        flightSeivice.saveOrUpdate(fttow);

        if (ftid != null){
            FlightTemplate fone = flightSeivice.findById(ftid);
            List<CabinInfoTemplate> css = fone.getCabinInfos();
            CabinInfoTemplate cts = new CabinInfoTemplate();
            int j = 0;
            for (CabinInfoTemplate ct : css) {

                if (ct.getId() == cid) {
                    ct.setCabin(ct.getCabin() - 1);
                    cts = ct;
                    break;
                }
                j++;
            }
            css.remove(j);
            css.add(cts);
            fone.setCabinInfos(css);
            flightSeivice.saveOrUpdate(fone);
        }
        return "/flight/paysuccess";
    }


}
